<div class="layuimini-main">
    <form class="layui-form" action="" lay-filter="edit">
        <input id="update_id" type="hidden" name="id"/>
        <div class="layui-form layuimini-form">
            <div class="layui-form-item">
                <label class="layui-form-label required">赛事名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" id="update_name" lay-verify="required" lay-reqtext="赛事名称不能为空"
                           placeholder="请输入赛事名称"
                           value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">赛事简介</label>
                <div class="layui-input-block">
                    <textarea name="introduction" id="update_introduction" placeholder="请输入"
                              class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">展示图片</label>
                <div class="layui-input-block">
                    <img src="" width="160" height="90" id="pic_show"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">更换图片</label>
                <div class="layui-input-block">
                    <input type="file" name="pic" placeholder="更换图片" value="" class="layui-input" id="pic">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label  required">赛事地点</label>
                <div class="layui-input-block">
                    <input type="text" name="place" id="update_place" placeholder="赛事地点" value="" class="layui-input"
                           lay-verify="required"
                           lay-reqtext="赛事地点不能为空">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label  required">主办方</label>
                <div class="layui-input-block">
                    <input type="hidden"  id="update_organizerId" >
                    <select name="organizerId" id="update_organizerId_select" lay-verify="">

                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label  required">承办方</label>
                <div class="layui-input-block">
                    <input type="text" name="host" id="update_host"
                           placeholder="承办方" value="" class="layui-input" lay-verify="required"
                           lay-reqtext="承办方不能为空">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">指导方</label>
                <div class="layui-input-block">
                    <input type="text" name="guide" id="update_guide" placeholder="指导方" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">执行方</label>
                <div class="layui-input-block">
                    <input type="text" name="executive" id="update_executive" placeholder="执行方" value=""
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">赞助方</label>
                <div class="layui-input-block">
                    <input type="text" name="sponsor" id="update_sponsor" placeholder="赞助方" value=""
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label  required">赛事联系人+电话</label>
                <div class="layui-input-block">
                    <input type="text" name="eventRelationPerson" id="update_eventRelationPerson" placeholder="赛事联系人+电话"
                           value="" class="layui-input"
                           lay-verify="required" lay-reqtext="联系人电话不能为空">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label  required">比赛开始时间</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="raceBeginTime" id="update_raceBeginTime"
                           placeholder="yyyy-MM-dd HH:mm:ss"
                           lay-verify="required" lay-reqtext="比赛开始时间不能为空">
                </div>

                <label class="layui-form-label  required">比赛开始结束</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="raceEndTime" id="update_raceEndTime"
                           placeholder="yyyy-MM-dd HH:mm:ss"
                           lay-verify="required" lay-reqtext="比赛开始结束不能为空">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label  required">报名开始结束</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="partBeginTime" id="update_partBeginTime"
                           placeholder="yyyy-MM-dd HH:mm:ss"
                           lay-verify="required" lay-reqtext="报名开始结束不能为空">
                </div>

                <label class="layui-form-label  required">报名开始结束</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="partEndTime" id="update_partEndTime"
                           placeholder="yyyy-MM-dd HH:mm:ss"
                           lay-verify="required" lay-reqtext="报名开始结束不能为空">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="status" id="update_status" lay-skin="switch" value="1" lay-text="生效|失效"
                           checked>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图文详情</label>
                <div class="layui-input-block">
                    <textarea name="detail" id="update_detail"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </div>
    </form>
</div>
<script src="/js/ckeditor/ckeditor.js"></script>
<script>

    if (CKEDITOR.instances['update_detail']) {
        CKEDITOR.remove(CKEDITOR.instances['update_detail']);
        layui.$("#cke_ckeditor").remove();
    }
    var editor = CKEDITOR.replace('update_detail', {allowedContent: true});

</script>
<script>

    editData = {}

    layui.use(['form', 'table', 'laydate'], function () {
        var laydate = layui.laydate;
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        $(".layui-layer-shade").css("z-index", "9999");
        $(".layui-layer-page").css("z-index", "9999");
        laydate.render({
            elem: '#update_raceBeginTime'
            , type: 'datetime'
        });
        laydate.render({
            elem: '#update_raceEndTime'
            , type: 'datetime'
        });
        laydate.render({
            elem: '#update_partBeginTime'
            , type: 'datetime'
        });
        laydate.render({
            elem: '#update_partEndTime'
            , type: 'datetime'
        });
        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            if (undefined === data.field.status) {
                data.field.status = 0
            }
            data.field.detail =
                editor.document.getBody().getHtml();
            console.log(data.field)
            let file = document.getElementById("pic").files[0];
            let myform = new FormData();
            myform.append('upload',file);
            $.ajax({
                url: "/uploadFile",
                method: "post",
                async: false,
                contentType: false,
                processData: false,
                data: myform,
                success: function (res) {
                    data.field.pic = res.url
                },
                error:function (res) {
                    console.log(res)
                }
            })

            $.ajax({
                url: "/event/information/update",
                method: "post",
                contentType: "application/json",
                dataType: "json",
                async: false,
                data: JSON.stringify(data.field),
                success: function (res) {
                    if (res.status === 1) {
                        layer.alert("成功", {icon: 1});
                    } else {
                        layer.alert("失败", {icon: 2});
                    }

                }
            })

            return false;
        });
        $.ajax({
            url: "/organizer/list",
            method: "post",
            contentType: "application/json",
            dataType: "json",
            success: function (res) {
                let organizers = [] = res.data;
                if (organizers.length !== 0) {
                    for (let i = 0; i < organizers.length; i++) {
                        console.log(organizers[i].introduction)
                        $("select[name=organizerId]").append("<option value=" + organizers[i].id + ">" + organizers[i].introduction + "</option>")
                    }

                }
                form.render();
            }
        })
        loadIndex = layer.load(1, {
            shade: [0.5, '#fff']
        });
        setTimeout(function () {
                console.log( $("#update_organizerId").val());
                var select = 'dd[lay-value=' + $("#update_organizerId").val() + ']';
                console.log(select)
                $('#update_organizerId_select').siblings("div.layui-form-select").find('dl').find(select).click();
            layer.close(loadIndex);
            },1000
        )
        form.render();
    });
</script>